/**
 * gulpfile.js/exec
 */

const Promise = require('bluebird');
const child_process = require('child_process');

const exec = function(cmd, options) {
    return new Promise((resolve, reject) => {
        options = Object.assign({
            cwd: process.cwd(),
            env: process.env
        }, options);

        child_process.exec(cmd, options, (err, stdout, stderr) => {
            if(err) {
                var errMsgs = [
                    '!!-- Exec Error --!!',
                    'Cmd: ['+ cmd +']',
                    'Error: ' + err,
                    'Stderr: ' + stderr,
                    'Stdout: ' + stdout,
                    '!!-- Exec Error End --!!'
                ];

                console.error(errMsgs.join('\n'));

                reject(err);
                return;
            }

            resolve(stdout);
        });
    });
};

module.exports = exec;